package com.zxmr.util;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.zxmr.domain.Host;
import com.zxmr.domain.component.Action;
import com.zxmr.domain.component.ActionGroup;
import com.zxmr.domain.component.Component;
import com.zxmr.domain.component.ComponentDescription;
import com.zxmr.domain.component.ComponentInstallInfo;
import com.zxmr.domain.component.Page;
import com.zxmr.domain.service.ComponentService;

public class ComponentController {

	public static void publishComponent(ComponentInstallInfo component)
	{
		// first query all clients information
		List<Host> hosts = queryAllHosts();
		
		// then sync the component data to all hosts
		// and also need to sync the static files to all client side
		for(Host host : hosts)
		{
			publishComponentToClient(host,component);
		}
	}
	public static void publicComponent(String url)
	{
		url = url + File.separator+"application.xml";
		ComponentDescription des = loadComponentDescription(url);
		
		ComponentService ser = new ComponentService();
		System.out.println(ser.addComponent(des));
	}
	public static void repairComponent(Component component)
	{
		
	}
	public static void deleteComponent(Component component)
	{
		
	}
	private static List<Host> queryAllHosts()
	{
		return null;
	}
	private static void publishComponentToClient(Host client,ComponentInstallInfo component)
	{
		
	}
	/**
	 * xml template as below:
	 * <?xml>
	 *<application>
	 *<name>component</name>
	 *<desc>sdfds</desc>
	 *
	 *<actions>
	 *	<action name="getall" display="查询全部"/>
	 *	<action name="getall1" display="查询1"/>
	 *	<action name="getall2" display="查询2"/>
	 *	<action name="getall3" display="查询3"/>
	 *</actions>
	 *
	 *</application>
	 * @param url
	 * @return
	 */
	public static ComponentDescription loadComponentDescription(String url)
	{
		File inputXml=new File(url);
		SAXReader saxReader = new SAXReader();
		try
		{
			ComponentDescription cd = new ComponentDescription();
			Document document = saxReader.read(inputXml);
			Element application = document.getRootElement();
			
			Component component = parseComponent(application);
			cd.setComponent(component);
			cd.setActions(parseActionGroup(application));
			cd.setPages(parsePage(application));
			return cd;
		}catch(Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}
	private static Component parseComponent(Element application)
	{
		Component component = new Component();
		component.setName(application.elementText("name"));
		component.setDescription(application.elementText("desc"));
		component.setDisplay(application.elementText("display"));
		component.setQueueName(application.elementText("queueName"));
		component.setVersion(application.elementText("version"));
		String price = application.elementText("price");
		String days = application.elementText("days");
		component.setDays(Integer.parseInt(days));
		component.setPrice(Double.parseDouble(price));
		return component;
		
	}
	private static List<ActionGroup> parseActionGroup(Element application)
	{
		Element actionGroup = application.element("actionGroup");
		if(actionGroup == null)return null;
		
		@SuppressWarnings("unchecked")
		Iterator<Element> it = actionGroup.elementIterator("group");
		List<ActionGroup> subGroup = new ArrayList<ActionGroup>();
		while(it.hasNext())
		{
			Element e = it.next();
			subGroup.add(parseGroup(e));
			
		}
		return subGroup;
	}
	private static ActionGroup parseGroup(Element group)
	{
		ActionGroup actionGroup = new ActionGroup();
		
		actionGroup.setDisplay(group.attributeValue("display"));
		actionGroup.setName(group.attributeValue("name"));
		
		
		@SuppressWarnings("unchecked")
		Iterator<Element> it = group.elementIterator("action");
		List<Action> actions = new ArrayList<Action>();
		
		while(it.hasNext())
		{
			Element e = it.next();
			String actionName = e.attributeValue("name");
			String display = e.attributeValue("display");
			Action action = new Action();
			action.setName(actionName);
			action.setDisplay(display);

			actions.add(action);
			
		}
		
		actionGroup.setSubActions(actions);
		
		@SuppressWarnings("unchecked")
		Iterator<Element> subGroupIt = group.elementIterator("group");
		List<ActionGroup> subGroup = new ArrayList<ActionGroup>();
		while(subGroupIt.hasNext())
		{
			Element e = subGroupIt.next();
			subGroup.add(parseGroup(e));
		}
		actionGroup.setSubGroup(subGroup);
		return actionGroup;
	}
	private static List<Page> parsePage(Element application)
	{
		Element pagesElement = application.element("pages");
		if(pagesElement == null)return null;
		
		@SuppressWarnings("unchecked")
		Iterator<Element> it = pagesElement.elementIterator("page");
		List<Page> pages = new ArrayList<Page>();
		
		while(it.hasNext())
		{
			Element e = it.next();
			String name = e.attributeValue("name");
			String display = e.attributeValue("display");
			String url = e.attributeValue("url");
			String image = e.attributeValue("image");
			Page page = new Page();
			page.setName(name);
			page.setDisplay(display);
			page.setUrl(url);
			pages.add(page);
			page.setImage(image);
		}
		return pages;
	}
}
